package nl.foldervisualizer.controller
{
	import com.ericfeminella.collections.HashMap;
	
	import flash.errors.IllegalOperationError;
	
	import nl.foldervisualizer.events.UIEvent;
	
	/**
	 * Conroller that contains a HashMap of UIEvent.kind to function mappings. The functions
	 * are defined in the subclass.
	 * 
	 * To use the MultiActionController you must do the following:
	 * 1. create a subclass of this controller
	 * 2. Add (preferably) private functions to your subclass, with a single argument of UIEvent, which
	 * 	corresponds to actions initiated by the user. Preferably the functions should be private because the only
	 * way to invoke those functions is by a UIEvent. The UIEvent also contains all parameter, in the payload property,
	 * neccesary for the function to do his work.
	 * 3. In the constructor of your baseclass, add a mapping for every action, which maps a UIEventKind (type) property 
	 * to a function. See AppController for an example.
	 * 4. set the handleUIEvent of your controller (which is defined in the MultiActionController) as the eventListener on
	 * the container which must listen to UIEvents.
	 */
	public class MultiActionController extends BaseController
	{
		/**
		* Mappings between UIEvent.kind (see UIEventKind) to functions. Those functions are defined in
		 * the subclass.
		*/
		protected var _handlerMappings:HashMap = new HashMap();
		
		public function MultiActionController()
		{
			super();
		}
		
		/**
		 * When a UIEvent is fired, the MultiActionController does the following:
		 * 1. The multiactions controller handleUIEvent method is invoked.
		 * 2. The MultiActionController looks up the function based on the UIEvent.kind property
		 * 3. The MultiActionController throws an IllegalOperationError if there is no functionmapping defined for this event
		 * 4. If there is a mapping defined, the MultiActoinController calls the function and passes the UIEvent as a parameter
		 * 5. The MultiActionController stops the propagation for the UIEvent
		 */
		public function handleUIEvent(event:UIEvent) : void {
			var funct:Function = _handlerMappings.getValue(event.kind)
			if (funct==null) {
				trace("No function for [" + event.kind + "] is defined in the handlerMappings. Make sure you define this event to function mapping in the constructor of you base class. See the ASDocs of the MultiActionController.")
				throw new IllegalOperationError("No function for [" + event.kind + "] is defined in the handlerMappings. Make sure you define this event to function mapping in the constructor of you base class. See the ASDocs of the MultiActionController.")
			}
			funct.call(this, event)
			event.stopPropagation()
		}		
	}
}